<?php

/**
 * zDataTable actions.
 *
 * @package    sf_sandbox
 * @subpackage zDataTable
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class zDataTableActions extends sfActions
{
    /**
     * Executes index action
     *
     */
    public function executeIndex()
    {
    }

    public function executeHelloWorld()
    {

        // request parameter *****************************
/*            $bSortable_0	false
bSortable_1	false
bSortable_2	true
bSortable_3	false
iColumns	4
iDisplayLength	10
iDisplayStart	0
iSortCol_0	2
iSortDir_0	asc
iSortingCols	1
sColumns	userId,userName,email1,lastName
sEcho	2*/
// end request parameter *****************************


        $arr = array();
        $arr[] = array(
            'userId',
            'userName',
            'email1',
            'email1',
            'lastName'
        );

        $arr[] = array(
            "1",
            "2",
            "3",
            "3",
            "4"
        );

        $arr[] = array(
            "1",
            "2",
            "3",
            "3",
            "4"
        );

        $arr[] = array(
            "1",
            "2",
            "2",
            "3",
            "4"
        );

        /******   sorting  *******/
        $sOrder = "ORDER BY  ";
        for ($i=0 ; $i<intval($this->getRequestParameter('iSortingCols')); $i++)
        {
            if ($this->getRequestParameter('bSortable_'.intval($this->getRequestParameter('iSortCol_'.$i))) == "true")
            {
                $sOrder .= $aColumns[intval($this->getRequestParameter('iSortCol_'.$i))]."
                    ".mysql_real_escape_string($this->getRequestParameter('sSortDir_'.$i)).", ";
            }
        }

        $sOrder = substr_replace($sOrder, "", -2);
        if ($sOrder == "ORDER BY")
        {
            $sOrder = "";
        }

        $output = array(
            "sEcho" => intval(1),
            "iTotalRecords" => 100,
            "iTotalDisplayRecords" => 10,
            "aaData" => $arr
        );
        echo json_encode($output);

        //echo '{"aaData":[[5747,"860629525702","CHIEW","LIN LIN","0198551299","0.00",["1 card","6064245391744787"],5747,5747],[5748,"860515526328","LEE","HUI KAH","0198448823","0.00",["1 card","6064240933779710"],5748,5748],[5749,"871227525818","alice,lin siu","yip","0168091231","26.00",["3 cards","6064248911377110","6064246300545254","6064240896546786"],5749,5749],[5750,"850606136270","ALLY LING","CHEE NGIN","0168619870","664.00",["2 cards","6064246324739947","6064243354720873"],5750,5750],[5751,"870106525630","KELLY CHENG","MEE NA","0165752113","0.00",["1 card","6064244914579064"],5751,5751],[5752,"850424136288","LINDA LING","LEH KIONG","0178506677","0.00",["1 card","6064249073559374"],5752,5752],[5753,"821013135878","TANG","TSE SIA","0198186421","0.00",["1 card","6064243938649965"],5753,5753],[5754,"570504135625","TANG","KIE HOO","60138211804","0.00",["1 card","6064241135819961"],5754,5754],[5755,"880719135087","LAU","SEE PENG","60166172134","0.00",["1 card","6064240905160516"],5755,5755],[5756,"521227135254","LAU","SIEW HUNG","6084335894","0.00",["1 card","6064240297088892"],5756,5756]],"actionErrors":[],"actionMessages":[],"customerListingDto":null,"errorMessages":[],"errors":{},"fieldErrors":{},"firstName":"","gridColumns":["userId","nric","firstName","lastName","mobileNo","creditAmt","memberCardNos","userId","userId"],"iTotalDisplayRecords":3660,"iTotalRecords":3660,"lastName":"","loginSessionId":1,"merchant":"","mobileNo":"","nric":"","reload":false,"sColumns":"userId,nric,firstName,lastName,mobileNo,creditAmt,memberCardNos,userId,userId","sEcho":"1","successAddNewUrl":null,"successExitUrl":null,"successMessage":null,"texts":null}';
        return sfView::HEADER_ONLY;
    }

    public function executeEcashLog()
    {
        // request parameter *****************************
/*            $bSortable_0	false
bSortable_1	false
bSortable_2	true
bSortable_3	false
iColumns	4
iDisplayLength	10
iDisplayStart	0
iSortCol_0	2
iSortDir_0	asc
iSortingCols	1
sColumns	userId,userName,email1,lastName
sEcho	2*/
// end request parameter *****************************
        $sColumns = $this->getRequestParameter('sColumns');
        $aColumns = explode(",", $sColumns);

        $iColumns = $this->getRequestParameter('iColumns');

        $offset = $this->getRequestParameter('iDisplayStart');
        $sEcho = $this->getRequestParameter('sEcho');
        $limit = $this->getRequestParameter('iDisplayLength');
        $arr = array();

        /******   total records  *******/
        $c = new Criteria();
    	$c->add(TblAccountLedgerPeer::F_DIST_ID, $this->getUser()->getAttribute(Globals::SESSION_DISTID));
    	$c->addAnd(TblAccountLedgerPeer::F_TYPE, Globals::ACCOUNT_TYPE_ECASH);
        $totalRecords = TblAccountLedgerPeer::doCount($c);

        /******   total filtered records  *******/
        if ($this->getRequestParameter('filterAction') != "") {
            $c->addAnd(TblAccountLedgerPeer::F_ACTION, "%".$this->getRequestParameter('filterAction')."%", Criteria::LIKE);
        }
        $totalFilteredRecords = TblAccountLedgerPeer::doCount($c);

        /******   sorting  *******/
        for ($i=0 ; $i<intval($this->getRequestParameter('iSortingCols')); $i++)
        {
            if ($this->getRequestParameter('bSortable_'.intval($this->getRequestParameter('iSortCol_'.$i))) == "true")
            {
                if ("asc" == $this->getRequestParameter('sSortDir_'.$i)) {
                    $c->addAscendingOrderByColumn($aColumns[intval($this->getRequestParameter('iSortCol_'.$i))]);
                } else {
                    $c->addDescendingOrderByColumn($aColumns[intval($this->getRequestParameter('iSortCol_'.$i))]);
                }
            }
        }

        /******   pagination  *******/
        $pager = new sfPropelPager('TblAccountLedger', $limit);
        $pager->setCriteria($c);
        $pager->setPage(($offset / $limit) + 1);
        $pager->init();

        foreach($pager->getResults() as $result){
            $arr[] = array(
                $result->getFCredit(),
                $result->getFDebit(),
                $result->getFBalance(),
                $result->getFAction(),
                $result->getFCreatedDatetime()
            );
        }

        $output = array(
            "sEcho" => intval($sEcho),
            "iTotalRecords" => $totalRecords,
            "iTotalDisplayRecords" => $totalFilteredRecords,
            "aaData" => $arr
        );
        echo json_encode($output);

        return sfView::HEADER_ONLY;
    }

    public function executeTestData()
    {

    }
}